<?php

namespace app\api\controller;

use app\admin\model\Cate;
use app\admin\model\Content;
use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $banners = Db::table('fa_banner')->where('state',1)->select();
        foreach ($banners as &$v){
            $v['image']  = request()->domain().$v['image'];
        }
        $cates = Cate::where('state',1)->field('id,name')->select();
        $cates = collection($cates)->toArray();
        array_unshift($cates,['id'=> '','name' => '全部']);
        $data = [
            'mobile' => config('site.mobile'),
            'baners' => $banners,
            'cates'=> $cates,
        ];
        $this->success('请求成功',$data);
    }

    public function artList()
    {
        $cate_id = input('cate_id');
        $where = "";
        if(!empty($cate_id)){
            $where = " cate_id=".$cate_id;
        }
        $list = Content::where($where)->field('id,title,image,art_desc')->paginate()->toArray();
        foreach($list['data'] as &$v){
            $v['image'] = cdnurl($v['image']);
            $v['art_desc'] = empty($v['art_desc'])?'':$v['art_desc'];
        }
        $this->success('成功',$list);

    }

    public function detail()
    {
        $id = input('id');
        $data = Content::get($id);
        if($data){
            $data = $data->toArray();
            $data['image'] = cdnurl($data['image']);
            $data['file'] = cdnurl($data['file']);
            $data['content'] = ($data['content']);
        }
        $this->success('成功',$data);
    }
}
